package cn.zf233.xcloud.web;

import cn.zf233.xcloud.commom.Const;
import cn.zf233.xcloud.commom.R;
import cn.zf233.xcloud.exception.ShareException;
import cn.zf233.xcloud.service.FileService;
import cn.zf233.xcloud.service.ShareService;
import cn.zf233.xcloud.util.CommonUtil;
import cn.zf233.xcloud.vo.ShareVo;
import cn.zf233.xcloud.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zf233 on 2021/4/29
 * 分享请求控制类
 */
@Controller
@RequestMapping("/share")
public class ShareController {

    @Resource
    private FileService fileService;

    @Resource
    private ShareService shareService;

    // 文件分享
    @PostMapping(value = "/file")
    @ResponseBody
    public R<String> share(HttpSession session, Long fileid, Integer type, Integer timeout) {
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return R.createByErrorNeedLogin("登陆失效，请重新登录");
        }
        return shareService.shareFileDec(CommonUtil.assembleUser(currentUser), fileid, type, timeout);
    }

    // 获取文件分享信息
    @PostMapping(value = "/info")
    @ResponseBody
    public R<ShareVo> shareInfo(HttpSession session, Long fileid) {
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return R.createByErrorNeedLogin("登陆失效，请重新登录");
        }
        return shareService.getFileShare(CommonUtil.assembleUser(currentUser), fileid);
    }

    // 获取分享文件
    @RequestMapping(value = "/get/{userid}/{fileid}/{type}/{code}")
    public String getShareFile(@PathVariable("userid") Long userid,
                               @PathVariable("fileid") Long fileid,
                               @PathVariable("type") Integer type,
                               @PathVariable("code") String code) {
        R response = shareService.timeout(fileid, userid, type, code);
        if (response.isSuccess()) {
            R responseFile = fileService.download(CommonUtil.assembleUser(userid), fileid);
            if (responseFile.isSuccess()) {
                return "redirect:" + responseFile.getData().toString();
            }
            throw new ShareException(responseFile.getMsg());
        }
        throw new ShareException(response.getMsg());
    }

    @PostMapping(value = "/list")
    @ResponseBody
    public R<List<ShareVo>> shares(HttpSession session) {
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return R.createByErrorIllegalArgument("登陆失效，请重新登录");
        }
        return shareService.listShare(CommonUtil.assembleUser(currentUser));
    }

    @ResponseBody
    @PostMapping(value = "/remove")
    public R remove(HttpSession session, Long[] shareId) {
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return R.createByErrorIllegalArgument("登陆失效，请重新登录");
        }
        return shareService.remove(shareId);
    }

    @ResponseBody
    @PostMapping(value = "/extend")
    public R extend(HttpSession session, Long shareId, Integer type) {
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return R.createByErrorIllegalArgument("登陆失效，请重新登录");
        }
        return shareService.extend(shareId, type);
    }
}